<?php
  /**
   * Kontrollbase
   *
   * An open source MySQL monitoring and analytics application
   *
   * @package Kontrollbase
   * @author Matt Reid
   * @copyright Copyright (c) 2009 Matt Reid, Kontrollbase LLC
   * @link http://kontrollbase.com
   */

class Add extends Controller {

	function Add()
	{
		parent::Controller();	
	}

	function index() {
	  check();
	  
	  $this->load->view('add/index');
        }
	
	function addcommandbindings() {
	  $this->load->model('Model_main', 'main');

	  $g['commands'] = $this->main->get_command_list();
	  $g['templates'] = $this->main->get_template_list();
	  $g['root'] = $this->config->item('base_url');

	  $this->load->view('add/add_commandbinding',$g);
	}
	
	function subcommandbinding() {
	  //check();
	  $this->load->model('Model_main', 'main');
	  $this->load->model('Model_hostbindings', 'add');
	  $this->load->library('form_validation');  

	  $this->form_validation->set_rules('command_objects_id', 'command_objects_id', 'trim|required');
	  $this->form_validation->set_rules('template_list_id', 'template_list_id', 'trim|required');

	  $command_objects_id = $this->input->post('command_objects_id');
	  $template_list_id = $this->input->post('template_list_id');

	  log_message('debug', "add commandbinding: command_objects_id:$command_objects_id, template_list_id:$template_list_id");
	  
	  $g['root'] = $this->config->item('base_url');
	  
	  if ($this->form_validation->run() == FALSE) {    
	    log_message('debug', "Add controller: subcommandbinding form validation failed.");
	    echo "{success: false, errors: { reason: 'Form validation failed. Please retry.'}}";
	  }
	  else {
	    $state = $this->add->add_templatecommand(
						      $template_list_id,
						      $command_objects_id);
	    if($state == 0) {
	      log_message('debug', "Add subcommandbinding  controller: JSON = {success: true}");
	      echo "{success: true}"; 
	    }
	    elseif($state == 1) {
	      log_message('debug', "Add subcommandbinding failed: JSON = success: false, errors: { reason: 'Add failed. Probably a duplicate entry.' }}");
	      echo "{success: false, errors: { reason: 'Add failed due to failed transaction - possibly a duplicate key.' }}";
	    }
	    else {
	      show_error("This is a general failure message.");
	    }
	  }
	}
	  
}

?>
